/**
 * 产品资料维护
 * @author shixc
 * @date 2019/01/11
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table1,
        table_gwam = {},
        table_gwan = {GWAN001: ''},
        gwam001,
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'GWAM001', width: 150, title: '产品编号', fixed: 'left', sort: true, search: 'default'}
            , {field: 'GWAML003', width: 160, title: '品名', search: true}
            , {field: 'GWAML004', width: 160, title: '规格', search: true}
            , {field: 'GWAML005', width: 80, title: '助记码', search: true}
            , {field: 'GWAM005', width: 80, title: '类型',filterDatas: app.datas_('gwam005')}
            , {field: 'GWAM006', width: 150, title: '归属母料号'}
            , {field: 'GWAM007_NAME', width: 80, title: '产品分类', sort: true}
            , {field: 'GWAM004_NAME', width: 80, title: '产品系列', sort: true}
            , {
                field: 'GWAM028', width: 120, title: '产品生命周期',
                filterDatas: app.datas_('crm_gwai500_gwam028'), sort: true,
                search: true, searchOpts: {type: 'select', default: ''}
            }
            , {field: 'GWAM008', width: 80, title: '销售单位'}
            , {field: 'GWAM030', width: 80, title: '大小品号',filterDatas: app.datas_('crm_gwai500_gwam030')}
            /* , {field: 'GWAM009', width: 80, title: '计价单位'}
             , {field: 'GWAM010', width: 80, title: '销售批量'}
             , {field: 'GWAM011', width: 80, title: '最小销售批量'}
             , {field: 'GWAM012', width: 80, title: '保固月数'}
             , {field: 'GWAM013', width: 80, title: '保固天数'}*/
            , {field: 'GWAM015', width: 200, title: '图片链接', templet: function (data) {
                var list = data.GWAM015;
                list = '<a href="' + g_ctxPath +'/html/pc/gwai500_image.html?data=' + data.GWAM015 +'" target="_Blank" style="text-decoration:underline;color:blue;">' + list + '</a>';
                return list;},
                style: 'text-decoration:underline;color:blue;'}
            , {field: 'GWAM031', width: 200, title: '视频链接', event: 'videos',
                style: 'text-decoration:underline;color:blue;'}
            , {field: 'GWAM002', width: 80, title: '品牌'}
            , {field: 'GWAM029', width: 80, title: '子品牌'}
            , {field: 'GWAM018', width: 200, title: '测试文件链接', event: 'files',
                style: 'text-decoration:underline;color:blue;'}
            , {
                field: 'GWAMACTI', width: 80, title: '有效',
                filterDatas: app.datas_('sys_y_n'),search: true,
                searchOpts: {type: 'select', default: ''}
            },
            {field: 'GWAM014', width: 80, title: '备注'},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai500setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        cols1 = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'GWAN002', width: 160, title: '属性特征分类', fixed: 'left', sort: true}
            , {field: 'GWAIL003', width: 80, title: '说明'}
            , {field: 'GWAN003', width: 160, title: '属性特征值'}
            , {field: 'GWAJL004', width: 80, title: '说明'}
            , {
                field: 'GWANACTI', width: 80, title: '有效',
                filterDatas: app.datas_('sys_y_n')
            },
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai500_1setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai500SettingAction,
        gwai500_1SettingAction
    ;

    var Gwai500 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai500',
                _searchs: [{
                    id: 'gwai500', tableId: 'gwai500_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwai500_edit', tpl: '/gwai500_edit'
                }, {
                    dom: 'gwai500_1_edit', tpl: 'gwai500_1_edit'
                }, {
                    dom: 'common_open_sxtz', tpl: 'common/_open_sxtz'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai500_table'),
                    url: g_ctxPath + 'crm/gwai500/getList',
                    exportsUrl: g_ctxPath + 'crm/gwai500/exportsData',
                    exportsName: '产品资料数据',
                    title: '产品资料信息表',
                    method: 'post',
                    id: 'gwai500_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai500setting-action').innerHTML).render({}, function (string) {
                    gwai500SettingAction = string;
                });

                layui.laytpl(app.getByid(doc, 'script-gwai500_1setting-action').innerHTML).render({}, function (string) {
                    gwai500_1SettingAction = string;
                });

                table1 = layui.table;
                table1.render({
                    elem: app.getByid(doc, 'gwai500_1_table'),
                    url: g_ctxPath + 'crm/gwai500/getGwanList',
                    exportsUrl: g_ctxPath + 'crm/gwai500/exportsData',
                    exportsName: '产品属性特征值数据',
                    title: '产品属性特征值信息表',
                    method: 'post',
                    id: 'gwai500_1_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar1',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols1,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                var tipsIndex = null;

                table.on('tool(gwai500_table)', function (obj) {
                    if (obj.event === 'imgs') {
                        if(obj.data.GWAM015 != undefined) {
                            if(obj.data.GWAM015.length > 0) {
                                window.open('/wissun-smfile/' + obj.data.GWAM015);
                            }
                        }
                    } else if (obj.event === 'files') {
                        if(obj.data.GWAM018 != undefined) {
                            if(obj.data.GWAM018.length > 0) {
                                window.open('/wissun-smfile/' + obj.data.GWAM018);
                            }
                        }
                    }else if (obj.event === 'videos') {
                        if(obj.data.GWAM031 != undefined) {
                            if(obj.data.GWAM031.length > 0) {
                                window.open('/wissun-smfile/' + obj.data.GWAM031);
                            }
                        }
                    } else {
                        layer.tips(gwai500SettingAction, obj.td, {
                            tips: [3, '']
                            , time: 3000
                            , anim: 500
                            , maxWidth: 120
                            , skin: 'layui-table-tips'
                            , success: function (layero, index) {
                                layero.find('li').on('click', function () {
                                    switch ($(this).attr('inx')) {
                                        case '0':
                                            self._openTplFull({
                                                tag: 'gwai500_edit',
                                                tagData: {
                                                    obj: obj,
                                                    operation:'edit'
                                                },
                                                index: 0,
                                                openOpts: {
                                                    title: '产品资料编辑',
                                                    maxmin: true
                                                }
                                            });
                                            break;
                                        case '1':
                                            self.del(obj);
                                            break;
                                    }
                                    layer.close(index);
                                });
                            }
                        });
                    }
                });

                //监听行单击事件
                table.on('row(gwai500_table)', function (obj) {
                    table_gwan.GWAN001 = obj.data.GWAM001;
                    gwam001 = obj.data;
                    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                    self._reloadTable({id: 'gwai500_1_table', where: table_gwan});
                });

                //监听行双击事件
                table.on('rowDouble(gwai500_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai500_edit',
                        tagData: {
                            obj:obj,
                            operation:'detail'
                        },
                        index: 0,
                        openOpts: {
                            title: '产品编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai500_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai500_edit',
                                tagData: {
                                    is_add: true,
                                    operation: 'add'
                                },
                                openOpts: {
                                    title: '产品资料新增',
                                    maxmin: true
                                },
                                index: 0
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;
                    }
                });

                table1.on('tool(gwai500_1_table)', function (obj) {
                    layer.tips(gwai500_1SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwai500_1_edit',
                                            tagData: obj,
                                            index: 1,
                                            openOpts: {
                                                title: '产品属性特征资料编辑',
                                                area: ["600px", "400px"]
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delGwan(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                // //监听事件
                table1.on('toolbar(gwai500_1_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            if (gwam001 == undefined) {
                                app.showError('请先选择产品资料');
                            } else {
                                if (gwam001.GWAM005 == '0') {
                                    self._openTplFull({
                                        tag: 'gwai500_1_edit',
                                        tagData: {
                                            is_add: true,
                                            GWAM005: gwam001.GWAM005,
                                            GWAN001: gwam001.GWAM001
                                        },
                                        openOpts: {
                                            title: '产品属性特征资料新增',
                                            area: ["600px", "400px"]
                                        },
                                        index: 1
                                    });
                                } else {
                                    self._openTplFull({
                                        tag: 'common_open_sxtz',
                                        tagData: {
                                            GWAM001: gwam001.GWAM001,
                                            GWAM005: gwam001.GWAM005,
                                            GWAM006: gwam001.GWAM006
                                        },
                                        index: 2,
                                        openOpts: {
                                            title: '选择属性特征分类',
                                            area: ['600px', '430px']
                                        }
                                    });
                                }
                            }

                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatchGwan();
                                }
                            });
                            break;
                    }
                });

                //监听行双击事件
                table1.on('rowDouble(gwai500_1_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai500_1_edit',
                        tagData: obj,
                        index: 1,
                        openOpts: {
                            title: '产品属性特征资料编辑',
                            area: ["600px", "400px"]
                        }
                    });
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai500_table', where: table_gwam});
        },

        reloadDetailTableCurr: function () {
            self._reloadTable({id: 'gwai500_1_table', where: table_gwan});
        },

        del: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwai500/delGwam',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '产品资料信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        deleteDataBatch: function () {
            var checkStatus = table.checkStatus('gwai500_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWAM');
                    _temp.GWAMENT = v.GWAMENT;
                    _temp.GWAM001 = v.GWAM001;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwai500/deleteDataBatchGwam',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '产品资料信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        delGwan: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwai500/delGwan',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '产品属性特征信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadDetailTableCurr();
                            }
                        }
                    });
                }
            });

        },

        deleteDataBatchGwan: function () {
            var checkStatus = table.checkStatus('gwai500_1_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWAN');
                    _temp.GWANENT = v.GWANENT;
                    _temp.GWAN001 = v.GWAN001;
                    _temp.GWAN002 = v.GWAN002;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwai500/deleteDataBatchGwan',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '产品属性特征信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadDetailTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai500',
                    methods: {}
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwai500();

});